﻿--客服成单跟踪列表{数量}
CREATE proc [dbo].[proc_Orders_Track_GetList_Kf_Count]
	
	@MainProjectId int,
	@SText nvarchar(50),
	@StartDate nvarchar(50),
	@EndDate nvarchar (50),
	@State int, --全部 2 完成 1 未完成 0
	@UserName nvarchar(50)
as
begin

	select 
		count(Id)
	from 
		orders o
	where
		(Convert(varchar(10),FixtureDate,120)>=@StartDate and Convert(varchar(10),FixtureDate,120)<=@EndDate)
		and (isnull((Select CustomerName From Customer Where CustomerId=o.CustomerId),'...') like '%'+@SText+'%')
		and (@MainProjectId=0 or MainProjectId=@MainProjectId)
		and ((case when ((select count(project.id) from project where project.orderid=o.oid)>0) and  (select count(project.id) from project where project.orderid=o.oid and project.isfinish=0 )=0 then 1 else 0 end ) = @state or @state=2)
		and TrackKf = @UserName

	end
	RETURN
